package com.app.controller;

import com.app.model.AdminUserModel;
import com.app.model.MilitarySportsModel;
import com.app.model.PersonModel;
import com.app.service.AdminUserService;
import com.app.service.MilitarySportsService;
import com.app.util.CommonHelp;
import com.app.util.Result;
import com.app.util.TokenUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 军事体育
 */
@Slf4j
@RestController
@Controller
@RequestMapping("/api/sports")
public class MilitarySportsController {
    @Autowired
    private MilitarySportsService militarySportsService;
    @Autowired
    private AdminUserService adminUserService;


    //新增
    @PostMapping("/insertMilitarySports")
    public Result<Object> insertMilitarySports(@RequestBody MilitarySportsModel militarySportsModel) {
        try {
            if (militarySportsModel.getId() > 0) {
                militarySportsModel.setUpdateTime(CommonHelp.getNowTimestamp());
                militarySportsService.update(militarySportsModel);
            } else {
                militarySportsModel.setCreateTime(CommonHelp.getNowTimestamp());
                militarySportsService.insert(militarySportsModel);
            }
        } catch (Exception e) {
            log.info(e.toString());
            return Result.success(e);
        }
        return Result.success("操作成功");
    }

    //查询
    @GetMapping("/getMilitarySportsList")
    public Result<Object> getMilitarySportsList(Integer page, Integer limit, String name,Integer departmentId,Integer sortType) {
        return Result.success(militarySportsService.getMilitarySportsList(page, limit, name,departmentId,sortType));
    }

    //删除
    @GetMapping("/deleteMilitarySports")
    public Result<Object> deleteMilitarySportsList(Integer id) {
        try {
            militarySportsService.deleteMilitarySportsList(id);
        } catch (Exception e) {
            return Result.success(e);
        }
        return Result.success("操作成功");
    }

    @DeleteMapping("/deleteMilitarySportsByIds")//批量删除角色
    public Result<Object> deleteByIds(@RequestParam("ids") String ids,HttpServletRequest request) {
        // 校验token并获取用户信息
        AdminUserModel user = validateTokenAndGetUser(request);
        if (user == null) {
            return Result.error(406, "用户不存在");
        }

        // 执行批量删除操作
        return militarySportsService.deleteBatchByIds(ids);
    }

    // 校验token并获取用户信息的方法
    private AdminUserModel validateTokenAndGetUser(HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            throw new IllegalArgumentException("token不存在");
        }
        String userId = TokenUtils.verify(token);
        return adminUserService.getUserByUserId(Integer.parseInt(userId));
    }

    //通过id查
    @GetMapping("/selectMilitarySportsById")
    public Result<Object> selectMilitarySportsById(Integer id) {
        try {
            MilitarySportsModel militarySportsModel = militarySportsService.selectMilitarySportsById(id);
            return Result.success(militarySportsModel);
        } catch (Exception e) {
            return Result.success(e);
        }
    }
}
